package com.web44.ssm.config;

import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @Title: MybatisConfig
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2024/1/15 11:22
 */
public class MybatisConfig {

    // 主配置文件
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource,
                                                       PageInterceptor pageInterceptor) {
        SqlSessionFactoryBean ssf = new SqlSessionFactoryBean();
        ssf.setDataSource(dataSource);
        ssf.setPlugins(pageInterceptor);
        ssf.setTypeAliasesPackage("com.web44.ssm.bean");
        return ssf;
    }

    /**
     * 开启了mapper扫描
     * @return
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.web44.ssm.mapper");
        return mapperScannerConfigurer;

    }

    @Bean
    public PageInterceptor pageInterceptor() {
        PageInterceptor pageInterceptor = new PageInterceptor();
        return pageInterceptor;
    }
}
